package com.gs.librarian.model.entity;

import java.util.HashSet;
import java.util.Set;

import javax.annotation.Generated;
import javax.persistence.Column;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
import javax.persistence.Version;

/**
 * @author  sabuj.das
 * @MailTo  sabuj.das@gmail.com
 */
@Entity
@Table(name = "eb_user")
public class User implements java.io.Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 4340055365187250096L;

	
	private long userId;
	private String password;
	private String emailId;
	private String firstName;
	private String lastName;
	private String middleName;
	
	private Set<Role> roles = new HashSet<Role>();
	
	private AuditProperties auditProperties;
	private int versionNo;
	
	public User() {
	}

	/**
	 * @return
	 * @uml.property  name="userId"
	 */
	@Id
	@GeneratedValue
	@Column(name = "USER_ID", unique = true, nullable = false)
	public long getUserId() {
		return this.userId;
	}

	/**
	 * @param userId
	 * @uml.property  name="userId"
	 */
	public void setUserId(long userId) {
		this.userId = userId;
	}

	

	/**
	 * @return
	 * @uml.property  name="password"
	 */
	@Column(name = "PASSWORD", nullable = false)
	public String getPassword() {
		return this.password;
	}

	/**
	 * @param password
	 * @uml.property  name="password"
	 */
	public void setPassword(String password) {
		this.password = password;
	}

	/**
	 * @return
	 * @uml.property  name="emailId"
	 */
	@Column(name = "EMAIL_ID", unique = true, nullable = false, length = 45)
	public String getEmailId() {
		return this.emailId;
	}

	/**
	 * @param emailId
	 * @uml.property  name="emailId"
	 */
	public void setEmailId(String emailId) {
		this.emailId = emailId;
	}

	/**
	 * @return
	 * @uml.property  name="firstName"
	 */
	@Column(name = "FIRST_NAME", nullable = false, length = 50)
	public String getFirstName() {
		return this.firstName;
	}

	/**
	 * @param firstName
	 * @uml.property  name="firstName"
	 */
	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	/**
	 * @return
	 * @uml.property  name="lastName"
	 */
	@Column(name = "LAST_NAME", nullable = false, length = 50)
	public String getLastName() {
		return this.lastName;
	}

	/**
	 * @param lastName
	 * @uml.property  name="lastName"
	 */
	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	/**
	 * @return
	 * @uml.property  name="middleName"
	 */
	@Column(name = "MIDDLE_NAME", length = 50)
	public String getMiddleName() {
		return this.middleName;
	}

	/**
	 * @param middleName
	 * @uml.property  name="middleName"
	 */
	public void setMiddleName(String middleName) {
		this.middleName = middleName;
	}

	/**
	 * @return
	 * @uml.property  name="roles"
	 */
	@ManyToMany(fetch = FetchType.LAZY)
	@JoinTable(name = "user_role_map", 
		joinColumns = { @JoinColumn(name = "USER_ID") }, 
		inverseJoinColumns = { @JoinColumn(name = "ROLE_ID") })
	public Set<Role> getRoles() {
		return roles;
	}

	/**
	 * @param roles
	 * @uml.property  name="roles"
	 */
	public void setRoles(Set<Role> roles) {
		this.roles = roles;
	}

	/**
	 * @return
	 * @uml.property  name="versionNo"
	 */
	@Version
	@Column(name = "VERSION_NO", nullable = false, columnDefinition=" integer default 0 ")
	public int getVersionNo() {
		return this.versionNo;
	}

	/**
	 * @param versionNo
	 * @uml.property  name="versionNo"
	 */
	public void setVersionNo(int versionNo) {
		this.versionNo = versionNo;
	}

	/**
	 * @return
	 * @uml.property  name="auditProperties"
	 */
	@Embedded
	public AuditProperties getAuditProperties() {
		return auditProperties;
	}

	/**
	 * @param auditProperties
	 * @uml.property  name="auditProperties"
	 */
	public void setAuditProperties(AuditProperties auditProperties) {
		this.auditProperties = auditProperties;
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((emailId == null) ? 0 : emailId.hashCode());
		return result;
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (obj == null) {
			return false;
		}
		if (!(obj instanceof User)) {
			return false;
		}
		User other = (User) obj;
		if (emailId == null) {
			if (other.emailId != null) {
				return false;
			}
		} else if (!emailId.equals(other.emailId)) {
			return false;
		}
		return true;
	}

	

}
